package dahuiji.login.service;

import java.util.ArrayList;
import java.util.List;

import dahuiji.common.dao.BaceDao;
import dahuiji.common.entity.User;

public class LoginService {
	private BaceDao baceDao;

	public User login(String nameOremail, String password) {
		String sqlByName = "select u.user_id, u.USER_NAME from tb_dahuiji_user u where u.USER_NAME= "
				+ "'"+nameOremail+"'" + "and u.password= " + "'"+password+"'";
		String sqlByEmail = "select u.user_id, u.USER_NAME from tb_dahuiji_user u where u.EMAIL= "
				+ "'"+nameOremail+"'" + "and u.password= " + "'"+password+"'";
		User user = null;
		if (nameOremail != null && password != null) {
			baceDao = new BaceDao();
			List list = new ArrayList();
			list = baceDao.executeQuerySql(sqlByName);
			if (list.size() == 1) {
				user = new User();
				Object[] o = (Object[]) list.get(0);
				user.setId(Long.valueOf(o[0].toString()));
				user.setName(o[1].toString());
			} else if(user==null){
				list = baceDao.executeQuerySql(sqlByEmail);
				if (list.size() == 1) {
					user = new User();
					Object[] o = (Object[]) list.get(0);
					user.setId(Long.valueOf(o[0].toString()));
					user.setName(o[1].toString());
				}
			}

		}
		return user;
	}

}
